<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">B海冷链生鲜直送</h1>
      <h2 class="subtitle">系统登陆</h2>
      <a-form @submit.prevent="handleSubmit">
        <a-form-item>
          <a-input v-model:value="userid" placeholder="用户ID" />
        </a-form-item>
        <a-form-item>
          <a-input-password v-model:value="password" placeholder="密码" />
        </a-form-item>
        <a-form-item>
          <a-select v-model:value="role" placeholder="选择角色">
            <a-select-option value="customer">顾客</a-select-option>
            <a-select-option value="business">商家</a-select-option>
            <a-select-option value="admin">系统管理员</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button class="button-login" type="primary" html-type="submit">登录</a-button>
          <a-button calss="button-login" type="default" @click="handleRegister">注册</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script>
import request from '../util/request';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';

export default {
  name: 'LoginForm',
  setup() {
    const router = useRouter();

    const userid = ref('');
    const password = ref('');
    const role = ref('');

    const handleSubmit = () => {
      // console.log('用户名:', username.value);
      // console.log('密码:', password.value);
      // console.log('角色:', role.value);
      if(userid.value === '' || password.value === '' || role.value === '') {
        message.error('请填写用户名、密码和角色');
        return;
      }
      // if(typeof(userid) !== 'integer') {
      //   message.error('用户ID必须是数字');
      //   return;
      // }
      request.post('/login', {
        userid: userid.value,
        password: password.value,
        role: role.value,
      }).then((response) => {
        console.log('登录:', response.data);
        // 根据登录结果进行相应的操作
        if (response.data.userid !== "0") {
          // 登录成功，保存用户信息到sessionStorage
          sessionStorage.setItem('userid', response.data.userid);
          sessionStorage.setItem('role', response.data.role);
          // 根据角色跳转到相应的页面
          if (role.value === 'customer') {
            router.push('/customer/shop');
          } else if (role.value === 'business') {
            router.push('/business/myProd');
          } else if (role.value === 'admin') {
            router.push('/system');
          }
        } else {
          // 登录失败，显示错误信息
          message.error('登录失败，请检查用户ID、密码和角色设置');
        }
      }).catch((error) => {
        console.error('登录失败:', error);
        // 处理登录失败的情况
        message.error('登录失败(' + error.message + ')');
      });
    }

    const handleRegister = () => {
      // 跳转到注册页面
      router.push({ name: 'Register' });
    };

    return {
      userid,
      password,
      role,
      handleSubmit,
      handleRegister,
    };
  },
};
</script>

<style scoped>
@import '../assets/styles/login.css';

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-box {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  width: 400px;
  text-align: center;
}

.title {
  font-size: 28px;
  margin-bottom: 10px;
  color: #1890ff;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.subtitle {
  font-size: 20px;
  margin-bottom: 20px;
  color: #555;
}

.button-login {
  margin-right: 50px;
}
</style>